﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CommonLibraries.DeviceLibrary;
using Distribution.Service;
using Entities;

namespace DistributionApplication
{
    public partial class frmGPSSettings : frmBase
    {
        #region - Constructor -

        public frmGPSSettings()
        {
            InitializeComponent();
        }

        #endregion

        #region - Event handlers -

        private void frmGPSSettings_Load(object sender, EventArgs e)
        {
            #region Config form
            lblTitle.Text = "PPI | Settings";
            LButtonText= "OK";
            RButtonText = "Cancel";
            #endregion
            lblError.Visible = false;
            LoadCombos();


            if (Settings.Timezone.Value != Int32.MinValue)
                ddlTimezone.SelectedIndex = ddlTimezone.Items.IndexOf((Settings.Timezone.Value  >= 0 ? "+" : "") + Settings.Timezone.Value);
        }

        protected override void lblRButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        protected override void lblLButton_Click(object sender, EventArgs e)
        {
            if (!ValidateResults())
                return;

            Settings.Timezone.Value = ddlTimezone.SelectedIndex > -1 ? Int32.Parse(ddlTimezone.Items[ddlTimezone.SelectedIndex].ToString()) : Int32.MinValue;
            Settings.UtilityEntity = (ELookUp)ddlUtility.SelectedItem;
            Settings.UserEntity = (EUser) ddlContact.SelectedItem;
            Settings.ProjectEntity = (EProject) ddlProject.SelectedItem;
            Settings.VendorEntity = (EVendor) ddlVendor.SelectedItem;
            Settings.ReceptionMode.Value = ddlMode.SelectedIndex;

            LSetting.Save(Settings);
            m_GPS.ChangeToMode((ReceptionMode)Settings.ReceptionMode.Value);
            lblRButton_Click(this, EventArgs.Empty);
        }

        private void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<EUser> users = LUser.GetAll(ddlVendor.SelectedItem as EVendor);
            MobileUIHelper.FillDropdown(ddlContact, users.ToArray(), null);

            List<EProject> projects = LProject.GetAll(ddlVendor.SelectedItem as EVendor);
            MobileUIHelper.FillDropdown(ddlProject, projects.ToArray(), null);
        }

        #endregion

        #region - Private methods -

        private bool ValidateResults()
        {
            bool _isValid = true;
            StringBuilder _Message = new StringBuilder();

            if (Settings.Timezone.Value == Int32.MinValue)
            {
                _isValid = false;
                _Message.AppendLine("Select a TimeZone");
            }

            if (ddlUtility.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select an Utility");
            }

            if (ddlContact.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select a Contact");
            }

            if (ddlProject.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select a Project");
            }

            if (ddlVendor.SelectedIndex < 0)
            {
                _isValid = false;
                _Message.AppendLine("Select a Vendor");
            }

            if (!_isValid)
                MessageBox.Show(_Message.ToString());

            return _isValid;
        }

        private void LoadCombos()
        {
            ddlTimezone.Items.Clear();
            for (Int32 _I = -12; _I <= 12; _I++)
                ddlTimezone.Items.Add((_I >= 0 ? "+" : string.Empty) + _I);
            ddlTimezone.SelectedIndex = 0;

            List<ELookUp> utilities = LLookUpService.GetAll(LookUpType.UtilityName);
            MobileUIHelper.FillDropdown(ddlUtility, utilities.ToArray(), null);

            List<EVendor> vendors = LVendor.GetAll();
            MobileUIHelper.FillDropdown(ddlVendor, vendors.ToArray(), null);

            List<EUser> users = LUser.GetAll(Settings.VendorEntity);
            MobileUIHelper.FillDropdown(ddlContact, users.ToArray(), null);

            List<EProject> projects = LProject.GetAll(Settings.VendorEntity);
            MobileUIHelper.FillDropdown(ddlProject, projects.ToArray(), null);

            ddlMode.Items.Clear();
            ddlMode.Items.Add("A Mode");
            ddlMode.Items.Add("B Mode");

            ddlVendor.SelectedItem = Settings.VendorEntity;
            ddlMode.SelectedIndex = Settings.ReceptionMode.Value;
            ddlContact.SelectedItem = Settings.UserEntity;
            ddlProject.SelectedItem = Settings.ProjectEntity;
            ddlUtility.SelectedItem = Settings.UtilityEntity;
        }

        #endregion
    }
}